#
# fusion.db.view
#
# Copyright 2007-2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import with_statement

from UserDict import DictMixin

from fusion.core.item import hex
from fusion.db.path import path_to_list

class View(DictMixin):
    def __init__(self, db, iid, base_path, type=None, cipher=None):
        iid = hex(iid)
        base_path = path_to_list(base_path)
        assert '!' not in base_path

        self.db = db
        self.iid = iid
        self.base_path = base_path
        self.type = type
        self.cipher = cipher

    def __getitem__(self, path):
        path = self.base_path + path_to_list(path)
        value = self.db.get(self.iid, path, type=self.type, cipher=self.cipher)
        if value is None:
            raise KeyError
        else:
            return value

    def __setitem__(self, path, value):
        # Could as well use post().
        path = self.base_path + path_to_list(path)
        self.db.put(self.iid, path, value, cipher=self.cipher)

    def __delitem__(self, path):
        # Should really raise KerError if the item does not exist.
        path = self.base_path + path_to_list(path)
        self.db.delete(self.iid, path)

    def keys(self):
        return self.db.keys(self.iid, self.base_path)

    #
    # Optimizations only
    #

    def has_key(self, path):
        path = self.base_path + path_to_list(path)
        try:
            self.db.get_size(self.iid, path)
        except KeyError:
            return False
        return True

    def values(self):
        with self.db.transaction():
            return DictMixin.values(self)

    def items(self):
        with self.db.transaction():
            return DictMixin.items(self)

    def clear(self):
        with self.db.exclusive_transaction():
            DictMixin.clear(self)

    def setdefault(self, key, default=None):
        with self.db.exclusive_transaction():
            return DictMixin.setdefault(self, key, default)

    def pop(self, key, *args):
        with self.db.exclusive_transaction():
            return DictMixin.pop(self, key, *args)

    def popitem(self):
        with self.db.exclusive_transaction():
            return DictMixin.popitem(self)

    def update(self, other=None, **kwargs):
        with self.db.exclusive_transaction():
            DictMixin.update(self, other, **kwargs)

    def __repr__(self):
        with self.db.transaction():
            return DictMixin.__repr__(self)

    def __cmp__(self, other):
        with self.db.transaction():
            return DictMixin.__cmp__(self, other)
